﻿namespace Dcms.UpdateModule
{
    using Dcms.BaseClass;
    using Dcms.Common;
    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;

    public class DcmsTempCreate : DcmsTempBaseCreate
    {
        public override void CreateFile(string FileContnet, string FileName, string FilePath)
        {
            try
            {
                string mapPath = Utils.GetMapPath(FilePath);
                DirectoryInfo info = new DirectoryInfo(mapPath);
                if (!info.Exists) info.Create();
                using (FileStream stream = new FileStream(mapPath + @"\" + FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(FileContnet);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                    stream.Dispose();
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        public int CreateTempLate(string FileName, string FilePath, string TempFolderName)
        {
            int num = 0;
            if (!File.Exists(FilePath)) return num;
            if (FileName.LastIndexOf('.') <= 0) return num;
            string htmlStr = "";
            string fileName = FileName.Substring(0, FileName.LastIndexOf('.'));
            string str3 = FileName.Substring(FileName.LastIndexOf('.'), FileName.Length - FileName.LastIndexOf('.')).ToLower();
            if (str3 != ".htm" && str3 != ".html" && str3 != ".asp" && str3 != ".aspx" && str3 != ".ascx" && !(str3 == ".shtml")) return num;
            htmlStr = DcmsFileManagerProcessor.ReadTextFile(FilePath, "utf-8");
            if (FileName.IndexOf("_") == 0) return this.CreateTempToAscx(htmlStr, fileName, TempFolderName);
            return this.CreateTempToAspx(htmlStr, fileName, TempFolderName);
        }

        public int CreateTempToAscx(string HtmlStr, string FileName, string TempFolderName)
        {
            try
            {
                StringBuilder builder = new StringBuilder();
                base.AspxHeadStr.Append("<%@ Control Language=\"C#\" AutoEventWireup=\"true\" EnableViewState=\"false\" %>" + Environment.NewLine);
                HtmlStr = base.SplitRepeaterBlock(HtmlStr, TempFolderName, FileName);
                HtmlStr = this.TempOtherTagConvert(HtmlStr, "{System.Controls,(?<FileName>([^}]*)?)}", "SysControl");
                HtmlStr = this.TempOtherTagConvert(HtmlStr, "<!--[^>]*File(\\s)*=(\\s)*(\")*(\\s)*(?<FileName>(\\w+)?)\\.\\w+(\\s)*(\")*(\\s)*[^>]*-->", TempFolderName);
                builder.Append(base.AspxHeadStr.ToString());
                builder.Append(HtmlStr);
                this.CreateFile(builder.ToString(), FileName + ".ascx", base.AscxPageDir + TempFolderName);
                base.AspxHeadStr.Remove(0, base.AspxHeadStr.Length);
                return 0;
            }
            catch (Exception)
            {
                return 1;
            }
        }

        public int CreateTempToAspx(string HtmlStr, string FileName, string TempFolderName)
        {
            try
            {
                StringBuilder builder = new StringBuilder();
                base.AspxHeadStr.Append("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" EnableViewState=\"false\" %>" + Environment.NewLine);
                HtmlStr = base.SplitRepeaterBlock(HtmlStr, TempFolderName, FileName);
                HtmlStr = this.TempOtherTagConvert(HtmlStr, "{System.Controls,(?<FileName>([^}]*)?)}", "SysControl");
                HtmlStr = this.TempOtherTagConvert(HtmlStr, "<!--[^>]*File(\\s)*=(\\s)*(\")*(\\s)*(?<FileName>(\\w+)?)\\.\\w+(\\s)*(\")*(\\s)*[^>]*-->", TempFolderName);
                builder.Append(base.AspxHeadStr.ToString());
                builder.Append(HtmlStr);
                this.CreateFile(builder.ToString(), FileName + ".aspx", base.AspxPageDir + TempFolderName);
                base.AspxHeadStr.Remove(0, base.AspxHeadStr.Length);
                return 0;
            }
            catch (Exception)
            {
                return 1;
            }
        }

        public override string FormatStrProcess(string CurrIndex, int Step)
        {
            return Utils.FormatStrProcess(CurrIndex, Step);
        }

        public override string GetSqlStr(string ConditionStr, string ItemStyleHtml)
        {
            return DcmsAllModelSqlManage.CreateSqlStr(ConditionStr, ItemStyleHtml);
        }

        protected string TempOtherTagConvert(string HtmlStr, string RegexStr, string TempFolderName)
        {
            StringBuilder builder = new StringBuilder();
            MatchCollection matchs = new Regex(RegexStr, RegexOptions.IgnoreCase).Matches(HtmlStr);
            if (matchs.Count > 0)
            {
                for (int i = 0; i < matchs.Count; i++)
                {
                    base.AspxHeadStr.Append("<%@ Register Src=\"" + base.AscxPageDir + TempFolderName + "/" + matchs[i].Groups["FileName"].Value + ".ascx\" TagName=\"" + matchs[i].Groups["FileName"].Value + "\" TagPrefix=\"Dcms\" %>" + Environment.NewLine);
                    HtmlStr = HtmlStr.Replace(matchs[i].Value, "<Dcms:" + matchs[i].Groups["FileName"].Value + " ID=\"" + matchs[i].Groups["FileName"].Value + "\" runat=\"server\" />");
                }
            }
            return HtmlStr;
        }
    }
}

